package service

import (
	"bytes"
	"encoding/base64"
	"fmt"
	"github.com/dchest/captcha"
	"goconfcenter/service/dto/common"
)

type SecurityService struct {
}

func SecurityServiceInstance() *SecurityService {
	var securityService *SecurityService
	return securityService
}

// GenImgCode 生成图片验证码
func (c *SecurityService) GenImgCode(width, height, codeCount, circleCount int) common.ImgCodeDTO {
	var randomImgDTO common.ImgCodeDTO
	captchaId := captcha.NewLen(codeCount) //设置验证码长度
	var image bytes.Buffer                 //图片信息保存在bytes.buff里面
	_ = captcha.WriteImage(&image, captchaId, width, height)
	randomImgDTO.UuId = captchaId
	randomImgDTO.Code = captchaId
	randomImgDTO.Img = fmt.Sprintf("data:image/png;base64," + base64.StdEncoding.EncodeToString([]byte(image.String())))
	//redis.Set(redis.GetRedisKey(redis.ImageCodeUdId), captchaId, redis.EXPIRED_MEDIUM)
	return randomImgDTO
}

//VerifyImgCode 验证图片验证码
func (c *SecurityService) VerifyImgCode(ImgUuid, ValidCode string) bool {
	return captcha.VerifyString(ImgUuid, ValidCode)
}
